package com.taa.controller;

import com.taa.dto.OrderCreateDTO;
import com.taa.dto.OrderDetailDTO;
import com.taa.dto.OrderSubmitDTO;
import com.taa.enums.ResponseCode;
import com.taa.exception.AppException;
import com.taa.response.Response;
import com.taa.service.IOrderService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: taa
 * @Desc: 订单服务
 * @Create: 2025-06-22 17:37
 **/
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    private IOrderService orderService;

    /**
     * 创建订单接口
     * @param createOrderDTO 请求体
     * @param userId 从请求参数中获取的用户ID
     * @return 包含订单号的响应
     */
    @PostMapping("/create")
    public Response<OrderSubmitDTO> createOrder(@RequestBody OrderCreateDTO createOrderDTO,
                                                @RequestParam("userId") Long userId) {
        try {
            log.info("创建订单请求开始, userId: {}", userId);
            OrderSubmitDTO orderSubmitDTO = orderService.createOrder(createOrderDTO, userId);
            log.info("订单创建成功, orderNo: {}", orderSubmitDTO.getOrderNo());
            return Response.<OrderSubmitDTO>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(orderSubmitDTO)
                    .build();
        } catch (AppException e) {
            log.error("创建订单业务异常, userId: {}", userId, e);
            return Response.<OrderSubmitDTO>builder()
                    .code(e.getCode())
                    .info(e.getInfo())
                    .build();
        } catch (Exception e) {
            log.error("创建订单系统异常, userId: {}", userId, e);
            return Response.<OrderSubmitDTO>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }

    /**
     * 根据订单号查询订单详情接口
     * @param orderNo 订单号
     * @param userId  当前登录用户ID
     * @return 订单详情
     */
    @GetMapping("/detail/{orderNo}")
    public Response<OrderDetailDTO> getOrderDetail(@PathVariable String orderNo,
                                                   @RequestParam("userId") Long userId) {
        try {
            log.info("查询订单详情请求开始, orderNo: {}, userId: {}", orderNo, userId);
            OrderDetailDTO orderDetail = orderService.getOrderDetailByOrderNo(orderNo, userId);
            log.info("查询订单详情成功, orderNo: {}", orderNo);
            return Response.<OrderDetailDTO>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(orderDetail)
                    .build();
        } catch (AppException e) {
            log.error("查询订单详情业务异常, orderNo: {}", orderNo, e);
            return Response.<OrderDetailDTO>builder()
                    .code(e.getCode())
                    .info(e.getInfo())
                    .build();
        } catch (Exception e) {
            log.error("查询订单详情系统异常, orderNo: {}", orderNo, e);
            return Response.<OrderDetailDTO>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }

    @PostMapping("/status/update")
    public Response<Void> updateOrderStatus(@RequestParam("orderNo") String orderNo,
                                            @RequestParam("status") Integer status) {
        try {
            log.info("接收到更新订单状态请求, orderNo: {}, status: {}", orderNo, status);
            orderService.updateOrderStatus(orderNo, status);
            return Response.<Void>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .build();
        } catch (AppException e) {
            log.error("更新订单状态业务异常, orderNo: {}", orderNo, e);
            return Response.<Void>builder()
                    .code(e.getCode())
                    .info(e.getInfo())
                    .build();
        } catch (Exception e) {
            log.error("更新订单状态系统异常, orderNo: {}", orderNo, e);
            return Response.<Void>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .build();
        }
    }
    
}

